package com.cityguardian.threads;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.cityguardian.cityguardianapp.AlertDetailActivity;
import com.cityguardian.ws.ConnectionThread;
import com.google.gson.Gson;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
import android.widget.LinearLayout.LayoutParams;

public class AsyncTaskBajarFoto extends AsyncTask<Integer, Double, String>{

	private String path;
	private String idAlerta;
	private AlertDetailActivity activity;
	
	public AsyncTaskBajarFoto(String path, String idAlerta, AlertDetailActivity activity){
		this.path=path;
		this.idAlerta=idAlerta;
		this.activity = activity;
		Log.d("path", path);
	}
	
	@Override
	protected String doInBackground(Integer... params) {
		
		String response="0";
		response = subirFoto();
		
		return response;
	}

	private String subirFoto() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
	    nameValuePairs.add(new BasicNameValuePair("idAlerta",idAlerta));
	    nameValuePairs.add(new BasicNameValuePair("foto",path));
	    
	    String responseBody="";
	    try{
	        HttpClient httpclient = new DefaultHttpClient();
	        HttpPost httppost = new HttpPost(ConnectionThread.privateRoute+"/ws/foto/cargarFoto");
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	        HttpResponse response = httpclient.execute(httppost);
	        responseBody = EntityUtils.toString(response.getEntity());
	        //Log.e("log_tag", responseBody);
	        httpclient.getConnectionManager().shutdown(); 
	    }
	    catch(Exception e){
	        e.toString();
	        return null;
	    }
	    //Log.d("FOTO",responseBody);
	    if (responseBody.equals("")){
			Log.e("FOTO", "Error in webservice");
			return null;
		}
	    
	    Gson gson = new Gson();
		String image = gson.fromJson(responseBody,String.class);
		byte[] bytarray = Base64.decode(image, Base64.DEFAULT);
		Bitmap mImageBitmap = BitmapFactory.decodeByteArray(bytarray, 0,
                bytarray.length);
	    
		float scaleFactor = (float)activity.scrollView.getWidth() / (float)mImageBitmap.getWidth();     
		mImageBitmap = Bitmap.createScaledBitmap(mImageBitmap, (int)(mImageBitmap.getWidth() * scaleFactor), (int)(mImageBitmap.getHeight() * scaleFactor), true);
		activity.fotoAlerta.setImageBitmap(mImageBitmap);
	    LayoutParams params = (LayoutParams) activity.fotoAlerta.getLayoutParams();             
	    params.width = mImageBitmap.getWidth();
	    params.height = mImageBitmap.getHeight();
	    activity.fotoAlerta.setLayoutParams(params);
		
		//return bmimage;
		return "0";
	}

}
